package mylib.dir;

/**
 * Created with IntelliJ IDEA.
 * User: 1
 * Date: 05.08.12
 * Time: 13:44
 * To change this template use File | Settings | File Templates.
 */
public class LineF implements Comparable<LineF> {
    public double A, B, C;

    public LineF(Point A, Point B) {
        this.A = B.y - A.y;
        this.B = A.x - B.x;
        this.C = -this.A * A.x - this.B * A.y;
        norm();
    }

    public LineF(PointF A, PointF B) {
        this.A = B.y - A.y;
        this.B = A.x - B.x;
        this.C = -this.A * A.x - this.B * A.y;
        norm();
    }

    public double signDistance(Point a) {
        return A * a.x + B * a.y + C;
    }

    public double distance(Point a) {
        return Math.abs(A * a.x + B * a.y + C);
    }

    private void norm() {
        double z = Math.sqrt(A * A + B * B);
        if (Math.abs(z - 1) > GeometryUtils.EPS && Math.abs(z) > GeometryUtils.EPS) {
            A /= z;
            B /= z;
            C /= z;
        }
        if (A < -GeometryUtils.EPS || Math.abs(A) < GeometryUtils.EPS && B < -GeometryUtils.EPS ||
                Math.abs(A) < GeometryUtils.EPS && Math.abs(B) < GeometryUtils.EPS && C < -GeometryUtils.EPS) {
            A = -A;
            B = -B;
            C = -C;
        }
    }

    @Override
    public int compareTo(LineF o) {
        if (Math.abs(A - o.A) > GeometryUtils.EPS)
            return Double.compare(A, o.A);
        if (Math.abs(B - o.B) > GeometryUtils.EPS)
            return Double.compare(B, o.B);
        return Double.compare(C, o.C);
    }
}
